<?php 
namespace app\index\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\user\traits\models\UserInfoCheck;
/**
 * 前台预定流程(已筛出资源)
 */
class Reserve extends Controller {
    use UserInfoCheck;
	use \app\common\traits\controller\TraitController;
    const DEPOSIT = '1';
    //前置操作
    protected $beforeActionList = [
        'checkLogin'=>['only' => 'getcaravandetail,getlogorderinfo'],
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect(url('/user/login','','','user'));
            exit;
        }
    }
    ///////////////////////交换房车流程///////////////////////////
    /**
     * [getCaravanDetail 资源详情界面]
     * @return [type] [description]
     */
    public function getCaravanDetail()
    { 
        //房车id
        $id = input('id');
        $ee = input('edate'); $ss = input('sdate');
        $this->assign('ee',$ee); $this->assign('ss',$ss);
        $str1=strtotime($ee);$str2=strtotime($ss);$days = ($str1-$str2)/(24*60*60)+1;
        $this->assign('days',$days);
        $share = model('Calendar')->getCaravanStatus($id);
        //$re = model('Calendar')->where('caravan_id',1)->setField('caravan_id','2');halt($re);
        //halt($share['shares']);
        $this->assign('share',$share['shares']);//获取日历表房车的分享状态
        $this->assign('disables',$share['disables']);//获取日历表房车的订单状态
        //押金
        $deposit = self::DEPOSIT;  $this->assign('deposit',$deposit);
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        $camp_id = $carinfo->services; $user_id = $carinfo->user_id;
        //营地信息
        $campinfo = model('camps')->where('id',$camp_id)->find();
        //房车拥有者头像
        $head = db('users')->where('id',$user_id)->value('head');
        //房车图片
        $a=$carinfo->piccaravan;
        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //房车封面
        $b = $carinfo->caravancover;
        $caravancover= $this->getEscapeSlash($b);
        $this->assign('caravancover',$caravancover);
        //钱数计算
        $travel_money_base = $campinfo->travel_money_base;
        $money = model('Orders')->getIntergral($ss,$ee,$travel_money_base);
        $this->assign('money',$money);
        return view('caravandetail',['carinfo'=>$carinfo,'campinfo'=>$campinfo,'head'=>$head]);
    }
    /**
     * [getJumpWhich 判断登陆状态的跳转界面]
     * @return [type] [description]
     */
    public function getJumpWhich()
    { 
        $id = input('id');$ee = input('edate'); $ss = input('sdate');//halt($id);
        //判断是否有不可住日期
        $query = model('Calendar')->where('caravan_id', $id)->where('datetimes', 'between', [$ss, $ee]);
        $res = $query->where(function($query1){
            $query1->whereOr(['orders' => ['in', '1,2'],'wishs'=>1,'shares'=>0]);
        })->count() ? 0 : 1;
        if (!$res) {
            return $this->error('亲，包含不可住日期',$url = null,'',$wait=3);
        }
        if (empty(session('user.id'))) {
            $this->redirect("/reserve/orderinfo?id={$id}&sdate={$ss}&edate={$ee}");
            exit;
        }else{
            $this->redirect("/reserve/logorderinfo?id={$id}&sdate={$ss}&edate={$ee}");
            exit;
        }
    }
    /**
     * [getOrderInfo 预定界面填写订单(未登录状态)]
     * @return [type] [description]
     */
    public function getOrderInfo()
    {
        //押金
        $deposit = self::DEPOSIT;
        //房车id获取房车信息
        $id = input('id');$ee = input('edate'); $ss = input('sdate');
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        //房车图片
        $a=$carinfo->piccaravan;
        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //总旅行币
        $camp_id =db('caravan')->where('id',$id)->value('services');
        $travel_money_base = db('camps')->where('id',$camp_id)->value('travel_money_base');
        $total_intergral = model('Orders')->getIntergral($ss,$ee,$travel_money_base);
        $this->assign('total_intergral',$total_intergral);
        //天数获取
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        return view('orderinfo',['deposit'=>$deposit,'carinfo'=>$carinfo,'ee'=>$ee,'ss'=>$ss,'days'=>$days]);
    }
    /**
     * [getLogOrderInfo 预定界面填写订单(登录状态)]
     * @return [type] [description]
     */
    public function getLogOrderInfo()
    {
        //房车id获取房车信息
        $id = input('id');$ee = input('edate'); $ss = input('sdate');//dump($id);
        $deposit =self::DEPOSIT;
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        //获取登录者联系人信息
        $user_id = session('user.id');
        //$user_id = 2;//测试
        $contacts = db('contactioninfo')->where('user_id',$user_id)->select();
        $this->assign('contacts',$contacts);
        //房车图片
        $a=$carinfo->piccaravan;

        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //总旅行币
        $camp_id =db('caravan')->where('id',$id)->value('services');//halt($camp_id);
        $travel_money_base = db('camps')->where('id',$camp_id)->value('travel_money_base');
        $total_intergral = model('Orders')->getIntergral($ss,$ee,$travel_money_base);
        $this->assign('total_intergral',$total_intergral);
        //登录者旅行币多少
        $integral = db('users')->where('id',$user_id)->value('integral');
        $this->assign('integral',$integral);
        return view('orderinfologin',['deposit'=>$deposit,'carinfo'=>$carinfo,'ee'=>$ee,'ss'=>$ss,'days'=>$days]);
    }
    //提交预定界面即生成订单（测试）
    public function getPay()
    {
        return view('paychose');
    }
    /**
     * [getCheckEmail 校验字段问题]
     * @return [type] [description]
     */
    public function getCheckEmail()
    {
        $email = input('email');
        $re = $this->cheack(['email'=>$email]);
        if ($re) {
            return 1;
        }else{
            return json(0);
        }
    }
    public function getCheckCode()
    {
        $code=input('imgCode');
        $captcha = new \think\captcha\Captcha(config('captcha'));
        if (!$captcha->check($code)) {
            return json(0);
        }
        return 1;
    }
    public function getCheckUserLogin()
    {
        if (!model('Users')->checkLogin()) {
            return json(0);
        }
        return 1;
    }
    /**
     * [getCheckDateAvailable 判断分享]
     * @return [type] [description]
     */
    public function getCheckDateAvailable()
    {
        $start_date = input('sdate');
        $end_date = input('edate');$caravan_id = input('id');
        $end_date = $end_date ? : $start_date;
        //房车id
        $query = model('Calendar')->where('caravan_id', $caravan_id)->where('datetimes', 'between', [$start_date, $end_date]);
        return  $query->where(function($query1){
            $query1->whereOr(['orders' => ['in', '1,2'],'wishs'=>1,'shares'=>0]);
        })->count() ? 0 : 1;
    }
    /**
     * [getCountDays 天数与钱数]
     * @return [type] [description]
     */
    public function getCountDays()
    {
        $ss = input('sdate');$ee = input('edate');$travel_money_base = input('m');
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        $money = model('Orders')->getIntergral($ss,$ee,$travel_money_base);
        $data = ['days'=>$days,'money'=>$money];
        return $data;
    }
    //////////////////////////////转义斜杠///////////////////////////
    public function getEscapeSlash($data_arr)
    {
        $data = str_replace("\\","/",$data_arr);
        return $data;
    }
    /////////////////////////////出租流程////////////////////////////
	//资源详情
    /**
     * [getCaravanDetail 资源详情界面]
     * @return [type] [description]
     */
    public function getRentCaravanDetail()
    { 
        Session::set('redirect_url', Request::instance()->url(true));
        //房车id
        $id = input('id');
        $ee = input('edate'); $ss = input('sdate');
        $this->assign('ee',$ee); $this->assign('ss',$ss);
        $str1=strtotime($ee);$str2=strtotime($ss);$days = ($str1-$str2)/(24*60*60)+1;
        $this->assign('days',$days);
        $share = model('Calendar')->getCaravanStatus($id);
        //$re = model('Calendar')->where('caravan_id',1)->setField('caravan_id','2');halt($re);改动日历表数据
        //halt($share['rents']);
        $this->assign('rent',$share['rents']);//获取日历表房车的出租状态
        $this->assign('disables',$share['disables']);//获取日历表房车的订单状态
        $renta = model('Calendar')->where('caravan_id',$id)->where('rents','1')->column('single_day_price', 'datetimes');
        $this->assign('renta',json_encode($renta));
        //押金
        $deposit = self::DEPOSIT;  $this->assign('deposit',$deposit);
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        $camp_id = $carinfo->services; $user_id = $carinfo->user_id;
        //营地信息
        $campinfo = model('camps')->where('id',$camp_id)->find();
        //房车拥有者头像
        $head = db('users')->where('id',$user_id)->value('head');
        //房车图片
        $a=$carinfo->piccaravan;
        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //房车封面
        $b = $carinfo->caravancover;
        $caravancover= $this->getEscapeSlash($b);
        $this->assign('caravancover',$caravancover);
        //总价格
        $total_money = model("Orders")->getMoney($id,$ss,$ee);
        $this->assign('total_money',$total_money);
        return view('rent/caravandetail',['carinfo'=>$carinfo,'campinfo'=>$campinfo,'head'=>$head]);
    }
    /**
     * [getCheckDateAvailable 判断出租]
     * @return [type] [description]
     */
    public function getCheckRentDate()
    {
        $start_date = input('sdate');
        $end_date = input('edate');$caravan_id = input('id');
        $end_date = $end_date ? : $start_date;
        //房车id
        $query = model('Calendar')->where('caravan_id', $caravan_id)->where('datetimes', 'between', [$start_date, $end_date]);
        return  $query->where(function($query1){
            $query1->whereOr(['orders' => ['in', '1,2'],'wishs'=>1,'rents'=>0]);
        })->count() ? 0 : 1;
    }
    /**
     * [getCountDays 天数与钱数]
     * @return [type] [description]
     */
    public function getRentCountDays()
    {
        $ss = input('sdate');$ee = input('edate');$caravan_id = input('id');
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        //同理
        // $mone = model('Calendar')->where('caravan_id', $id)->where('datetimes', 'between', [$ss, $ee])->field("single_day_price,SUM(single_day_price)as c")->find();
        // dump($mone->c);
        $mon = model('Calendar')->where('caravan_id', $caravan_id)->where('datetimes', 'between', [$ss, $ee])->column('single_day_price');
        $money = array_sum($mon);
        $data = ['days'=>$days,'money'=>$money];
        return $data;
    }
    //预定界面填写订单
     /**
     * [getJumpWhich 判断登陆状态的跳转界面]
     * @return [type] [description]
     */
    public function getRentJumpWhich()
    { 
        if (!model('Users')->checkLogin()) {
            return $this->error('请登录',$url = null,'',$wait=3);
        }
        $id = input('id');$ee = input('edate'); $ss = input('sdate');
        //判断是否有不可住日期
        $query = model('Calendar')->where('caravan_id', $id)->where('datetimes', 'between', [$ss, $ee]);
        $res =   $query->where(function($query1){
            $query1->whereOr(['orders' => ['in', '1,2'],'wishs'=>1,'rents'=>0]);
        })->count() ? 0 : 1;
        if (!$res) {
            return $this->error('亲，包含不可住日期',$url = null,'',$wait=3);
        }
        if (empty(session('user.id'))) {
            $this->redirect("/reserve/rentorderinfo?id={$id}&sdate={$ss}&edate={$ee}");
            exit;
        }else{
            $this->redirect("/reserve/rentlogorderinfo?id={$id}&sdate={$ss}&edate={$ee}");
            exit;
        }
    }
    /**
     * [getOrderInfo 预定界面填写订单(未登录状态)]
     * @return [type] [description]
     */
    public function getRentOrderInfo()
    {
        //押金
        $deposit = self::DEPOSIT;
        //房车id获取房车信息
        $id = input('id');$ee = input('edate'); $ss = input('sdate');
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        //房车图片
        $a=$carinfo->piccaravan;
        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //总价格
        $total_money = model("Orders")->getMoney($id,$ss,$ee);
        $this->assign('total_money',$total_money);
        //天数获取
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        return view('rent/orderinfo',['deposit'=>$deposit,'carinfo'=>$carinfo,'ee'=>$ee,'ss'=>$ss,'days'=>$days]);
    }
    /**
     * [getLogOrderInfo 预定界面填写订单(登录状态)]
     * @return [type] [description]
     */
    public function getRentLogOrderInfo()
    {
        //房车id获取房车信息
        $id = input('id');$ee = input('edate'); $ss = input('sdate');
        $deposit =self::DEPOSIT;
        $carinfo = model('CaravanInfos')->where('id',$id)->find();
        $str1=strtotime($ee);$str2=strtotime($ss);$days= ($str1-$str2)/(24*60*60)+1;
        //获取登录者联系人信息
        $user_id = session('user.id');
        //$user_id = 2;//测试
        $contacts = db('contactioninfo')->where('user_id',$user_id)->select();
        $this->assign('contacts',$contacts);
        //房车图片
        $a=$carinfo->piccaravan;
        $picCaravan= $this->getEscapeSlash($a);
        $this->assign('picCaravan',$picCaravan);
        //总价格
        $total_money = model("Orders")->getMoney($id,$ss,$ee);
        $this->assign('total_money',$total_money);
        //登录者旅行币多少
        $integral = db('users')->where('id',$user_id)->value('integral');
        $this->assign('integral',$integral);
        return view('rent/orderinfologin',['deposit'=>$deposit,'carinfo'=>$carinfo,'ee'=>$ee,'ss'=>$ss,'days'=>$days]);
    }
}